package com.fromfuture.xuhuidoc.android.activity.mainframe;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.Interface.Interface;
import com.fromfuture.xuhuidoc.android.adapter.MainFrameActivityViewPageFragmentAdapter;
import com.fromfuture.xuhuidoc.android.base.AppManager;
import com.fromfuture.xuhuidoc.android.base.BaseFragment;
import com.pgyersdk.update.PgyUpdateManager;

import java.io.IOException;
import java.util.ArrayList;

import app.auto.runner.ActivityBaseCompact;

public class MainFrameActivity extends ActivityBaseCompact implements ViewPager.OnPageChangeListener {

    private CheckBox checkBox01, checkBox02, checkBox03;
    private WorkBenchFragment fragment01;
    private DocCircleFragment fragment02;
    private MineFragment fragment03;
    public ViewPager viewPager;
    private MainFrameActivityViewPageFragmentAdapter mainFrameActivityViewPageFragmentAdapter;
    private ArrayList<Fragment> fragmentlist;

    private InputMethodManager imm;

    private BaseFragment mBackHandedFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_frame);

        init();
        AppManager.mainFrameActivity = this;
        verifyStoragePermissions(this);
        PgyUpdateManager.register(this);

//        Interface.getAppVersion(this);

//        if (Build.VERSION.SDK_INT >= 23) {
//            if (!Settings.canDrawOverlays(this)) {
//                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivityForResult(intent, 1);
//            } else {
//                //TODO 做你需要的事情
//            }
//        }

//        Interface.queryClinichistory();
        try {
            Interface.addbli("post","","", null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragment01.onActivityResult(requestCode, resultCode, data);
        fragment02.onActivityResult(requestCode, resultCode, data);
        fragment03.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
//            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA

    };

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }

    /**
     * 初始方法
     */
    @SuppressLint("WrongConstant")
    private void init() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        findViewById();
        fragment();
        CheckBoxOnClick();
        checkBox01.performClick();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        CheckBoxState(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 查找控件
     */
    private void findViewById() {
        viewPager = (ViewPager) findViewById(R.id.viewpage_mainfragactivity);
        checkBox01 = (CheckBox) findViewById(R.id.checkbox_workbench);
        checkBox02 = (CheckBox) findViewById(R.id.checkbox_docCircle);
        checkBox03 = (CheckBox) findViewById(R.id.checkbox_mine);
    }

    /**
     * fragment和viewpager的关系
     */
    private void fragment() {
        fragment01 = new WorkBenchFragment();
        fragment02 = new DocCircleFragment();
        fragment03 = new MineFragment();
        fragmentlist = new ArrayList<Fragment>();
        fragmentlist.add(fragment01);
        fragmentlist.add(fragment02);
        fragmentlist.add(fragment03);
        mainFrameActivityViewPageFragmentAdapter = new MainFrameActivityViewPageFragmentAdapter(getSupportFragmentManager(), fragmentlist);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(mainFrameActivityViewPageFragmentAdapter);
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }

    /**
     * checkbox点击事件
     */
    private void CheckBoxOnClick() {
        checkBox01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBoxState(0);
                viewPager.setCurrentItem(0);
            }
        });
        checkBox02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBoxState(1);
                viewPager.setCurrentItem(1);
            }
        });
        checkBox03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBoxState(2);
                viewPager.setCurrentItem(2);
            }
        });
        findViewById(R.id.layout1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkBox01.performClick();
            }
        });
        findViewById(R.id.layout2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkBox02.performClick();
            }
        });
        findViewById(R.id.layout3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkBox03.performClick();
            }
        });
    }

    /**
     * checkbox选择状态
     */
    @SuppressLint("NewApi")
    private void CheckBoxState(int position) {
        ((ImageView) findViewById(R.id.img1)).setImageResource(R.drawable.workbench_unchoosed);
        ((ImageView) findViewById(R.id.img2)).setImageResource(R.drawable.ysq_unchoosed);
        ((ImageView) findViewById(R.id.img3)).setImageResource(R.drawable.me_unchoosed);
        checkBox01.setTextColor(Color.parseColor("#666666"));
        checkBox02.setTextColor(Color.parseColor("#666666"));
        checkBox03.setTextColor(Color.parseColor("#666666"));
        switch (position) {
            case 10:
                checkBox01.setChecked(true);
                checkBox02.setChecked(false);
                checkBox03.setChecked(false);
                break;
            case 0:
                ((ImageView) findViewById(R.id.img1)).setImageResource(R.drawable.workbench_choosed);
                checkBox01.setTextColor(Color.parseColor("#2c8fff"));
                checkBox01.setChecked(true);
                checkBox02.setChecked(false);
                checkBox03.setChecked(false);
                break;
            case 1:
                ((ImageView) findViewById(R.id.img2)).setImageResource(R.drawable.ysq_choosed);
                checkBox02.setTextColor(Color.parseColor("#2c8fff"));
                checkBox01.setChecked(false);
                checkBox02.setChecked(true);
                checkBox03.setChecked(false);
                break;
            case 2:
                ((ImageView) findViewById(R.id.img3)).setImageResource(R.drawable.me_choosed);
                checkBox03.setTextColor(Color.parseColor("#2c8fff"));
                checkBox01.setChecked(false);
                checkBox02.setChecked(false);
                checkBox03.setChecked(true);
                break;
        }
        inPutHide();

    }

    /**
     * 隐藏软键盘
     */
    private void inPutHide() {
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }


}
